/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.chassis.ems;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;

import twaver.Grid;
import twaver.TWaverUtil;
import twaver.network.TNetwork;
import twaver.network.ui.GridUI;

public class EMSGridUI extends GridUI {

	public static final String PERCENT = "emsgridui.percent";
	public static final String GRADIENTTYPE = "emsgridui.gradienttype";

	public EMSGridUI(TNetwork network, Grid grid) {
		super(network, grid);
	}

	public void paintBody(Graphics2D g2d) {
		Rectangle rect = element.getBounds();
		Object percentObj = element.getClientProperty(PERCENT);
		double percent = 0;
		if (percentObj != null) {
			percent = ((Double) percentObj).doubleValue();
		}
		g2d.setColor(Color.DARK_GRAY);
		g2d.fill(rect);
		g2d.setColor(Color.WHITE);
		rect.grow(-2, -2);
		double x = rect.getX();
		double y = rect.getY();
		double height = rect.getHeight();
		double width = rect.getWidth();

		g2d.draw(rect);
		Rectangle2D.Double percentRect = new Rectangle2D.Double(x + 2, y + height * (1 - percent) + 0.5, width - 3, height * percent);
		Color color = Color.GREEN;
		if (percent > 0.6) {
			color = Color.ORANGE;
		}
		if (percent > 0.8) {
			color = Color.RED;
		}
		int factory = Integer.parseInt(element.getClientProperty(GRADIENTTYPE).toString());
		Paint paint = TWaverUtil.createGradientPaint(factory, rect, Color.WHITE, color);
		g2d.setPaint(paint);
		g2d.fill(percentRect);
	}
}